// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.io.IOException;
import java.nio.ByteBuffer;
import zombie.characters.IsoPlayer;
import zombie.core.logger.ExceptionLogger;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.core.skinnedmodel.visual.ItemVisuals;
import zombie.inventory.InventoryItem;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.ServerGUI;

public class SyncClothingPacket implements INetworkPacket {
    private IsoPlayer player = null;
    private String location = "";
    private InventoryItem item = null;

    public void set(IsoPlayer _player, String _location, InventoryItem _item) {
        this.player = _player;
        this.location = _location;
        this.item = _item;
    }

    public boolean isEquals(IsoPlayer _player, String _location, InventoryItem _item) {
        return this.player.OnlineID == _player.OnlineID && this.location.equals(_location) && this.item == _item;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        short _short = b.getShort();
        this.location = zombie.GameWindow.ReadString(b);
        byte _byte = b.get();
        if (_byte == 1) {
            try {
                this.item = InventoryItem.loadItem(b, 195);
            } catch (Exception exception) {
                exception.printStackTrace();
            }

            if (this.item == null) {
                return;
            }
        }

        if (GameServer.bServer) {
            this.player = (IsoPlayer)GameServer.IDToPlayerMap.get(_short);
        } else {
            this.player = (IsoPlayer)GameClient.IDToPlayerMap.get(_short);
        }

        if (this.player != null) {
            try {
                this.player.getHumanVisual().load(b, 195);
                this.player.getItemVisuals().load(b, 195);
            } catch (Throwable throwable) {
                ExceptionLogger.logException(throwable);
                return;
            }

            if (_byte == 1) {
                this.player.getWornItems().setItem(this.location, this.item);
            }

            if (GameServer.bServer && ServerGUI.isCreated() || GameClient.bClient) {
                this.player.resetModelNextFrame();
            }
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putShort(this.player.OnlineID);
        b.putUTF(this.location);
        if (this.item == null) {
            b.putByte((byte)0);
        } else {
            b.putByte((byte)1);

            try {
                this.item.saveWithSize(b.bb, false);
            } catch (IOException iOException0) {
                iOException0.printStackTrace();
            }
        }

        try {
            this.player.getHumanVisual().save(b.bb);
            ItemVisuals itemVisuals = new ItemVisuals();
            this.player.getItemVisuals(itemVisuals);
            itemVisuals.save(b.bb);
        } catch (IOException iOException1) {
            iOException1.printStackTrace();
        }
    }

    @Override
    public boolean isConsistent() {
        return this.player != null;
    }

    @Override
    public String getDescription() {
        String string;
        if (this.player == null) {
            string = "player=null";
        } else {
            string = String.format("player=%s(oid:%d)", this.player.username, this.player.OnlineID);
        }

        string = string + ", location=" + this.location;
        if (this.item == null) {
            string = string + ", item=null";
        } else {
            string = string + ", item=" + this.item.getFullType();
        }

        return string;
    }
}
